<template>
    <div class="common-box" v-if="boxIsShow">
        <div class="common-content-box">
            <div class="top_data_div">
                <div class="top_title_box"><div class="top_title">首页仪表盘</div></div>
                <div class="top_data_box" v-for="(d,index) in topDatas" :style="{ 'margin-right': index === topDatas.length - 1 ? '0' : '2%' }">
                    <div class="top_data_name">{{ d.title }}</div>
                    <div class="top_data_content"><span style="font-size: 12px;">{{ d.unit==1?'￥':'' }}</span>{{ d.content }}{{ d.unit==2?'%':'' }}</div>
                    <div class="top_data_infos">
                        <div class="top_data_infos_color" :style="{ 'background-color:': d.type === 1 ? 'green' : d.type === 2? 'red':'#ff9e5c' }"></div>
                        <div class="top_data_infos_content">{{d.infos}}{{d.type!=3?'%':''}}</div>
                    </div>
                </div>
            </div>
            <div class="midel_echart_box">
                <div class="midel_echart" style="width: 50%;margin-right: 2%;">
                    <div class="midel_echart_title_box">
                      <img src="@/assets/icon-data.png" class="midel_echart_title_icon">
                      <div class="midel_echart_title">销售趋势</div>
                    </div>
                    <div class="midel_echart_content" id="chartLeft"></div>
                </div>
                <div class="midel_echart" style="width: 48%;">
                  <div class="midel_echart_title_box">
                      <img src="@/assets/icon-data.png" class="midel_echart_title_icon">
                      <div class="midel_echart_title">订单信息</div>
                    </div>
                    <div class="midel_echart_content" id="chartRight"></div>
                </div>
            </div>
            <div class="bottom_echart_box">
              <div class="midel_echart_title_box">
                <img src="@/assets/icon-data.png" class="midel_echart_title_icon">
                <div class="midel_echart_title">产品销量表现</div>
                <div style="position: absolute;right: 2%;top:5px">
                  <el-date-picker
                    size="small "
                    v-model="searchMonth"
                    type="month"
                    placeholder="选择月份"
                    @change="handleMonthChange"
                    value-format="yyyy-MM">
                  </el-date-picker>
                </div>
              </div>
              <div class="midel_echart_content" id="chartBottom"></div>
            </div>
        </div>
    </div>
</template>
<script>
import * as echarts from 'echarts';
export default {
  data() {
    return {
      accountType:1,
      accountId:0,
      boxIsShow:true,
      searchMonth:'',
      topDatas:[],
      leftData1:[],
      leftData2:[],
      rightData:{
        num1:0,
        num2:0,
        num3:0
      }
    };
  },
  created() {
    
  },
  mounted(){
    this.__init();
  },
  methods: {
    __init() {
      this.accountType=Number(sessionStorage.getItem("supplierType"));
      this.accountId=Number(sessionStorage.getItem("supplierId"));
      this.getIndexTopData();
      this.getIndexLeftData();
      this.getIndexRightData();
      this.getIndexBottomData();
    },
    getIndexTopData(){
      var url="manager/getIndexTopData";
      var params={
        accountType: this.accountType,
        accountId: this.accountId
      }
      this.$post(url, params).then((res) => {
        console.log(res)
        if (res.code === 0) {
          this.topDatas=res.data;
        } else {
          this.$message.error(res.msg)
        }
      });
    },
    getIndexLeftData(){
      var url="manager/getIndexLeftData";
      var params={
        accountType: this.accountType,
        accountId: this.accountId
      }
      this.$post(url, params).then((res) => {
        console.log(res)
        if (res.code === 0) {
          this.leftData1=res.data.oMoneys;
          this.leftData2=res.data.oNums;
          this.initLeftChart();
        } else {
          this.$message.error(res.msg)
        }
      });
    },
    initLeftChart(){
      const chartLeftDom =document.getElementById('chartLeft'); 
      const myChart = echarts.init(chartLeftDom);
      const option = {
        xAxis: {
          type: 'category',
          data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月','八月','九月','十月','十一月','十二月']
        },
        legend: {
          data: ['收入', '订单数']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '收入',
            data: this.leftData1,
            type: 'line'
          },
          {
            name: '订单数',
            data: this.leftData2,
            type: 'line'
          }
        ]
      };
      option && myChart.setOption(option);
    },
    getIndexRightData(){
      var url="manager/getIndexRightData";
      var params={
        accountType: this.accountType,
        accountId: this.accountId
      }
      this.$post(url, params).then((res) => {
        console.log(res)
        if (res.code === 0) {
          this.rightData={
            num1:res.data.num1,
            num2:res.data.num2,
            num3:res.data.num3
          }
          this.initRightChart();
        } else {
          this.$message.error(res.msg)
        }
      });
    },
    initRightChart(){
      var chartRightDom = document.getElementById('chartRight');
      var myChart = echarts.init(chartRightDom);
      var option;

      option = {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '2%',
          left: 'right'
        },
        series: [
          {
            name: '订单状态',
            type: 'pie',
            radius: ['60%', '80%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
            },
            labelLine: {
              show: false
            },
            data: [
              { value: this.rightData.num1, name: '待发货' },
              { value: this.rightData.num2, name: '待收货' },
              { value: this.rightData.num3, name: '售后中' },
            ]
          }
        ]
      };

      option && myChart.setOption(option);
    },
    handleMonthChange(value){
      this.searchMonth=value;
      this.getIndexBottomData();
    },
    getIndexBottomData(){
      var url="manager/getIndexBottomData";
      var params={
        accountType: this.accountType,
        accountId: this.accountId,
        searchMonth:this.searchMonth
      }
      this.$post(url, params).then((res) => {
        console.log(res)
        if (res.code === 0) {
          this.bottomData1=res.data.nameList;
          this.bottomData2=res.data.salesNums;
          this.initBottomChart();
        } else {
          this.$message.error(res.msg)
        }
      });
    },
    initBottomChart(){
      var chartBottomtDom = document.getElementById('chartBottom');
      var myChart = echarts.init(chartBottomtDom);
      var option;

      option = {
        xAxis: {
          type: 'category',
          data: this.bottomData1
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: this.bottomData2,
            type: 'bar',
            barWidth: '15%'
          }
        ]
      };

      option && myChart.setOption(option);
    }
  }
}
</script>
<style scoped lang="scss">
  .common-box{
    background-color: #f9f9fb;
    height: 100%;
  }
  .common-content-box{
    height: 95%;
    margin-top: 2%;
    width: 96%;
    display: inline-block;
  }
  .top_data_div{
    height: 100px;
  }
  .top_data_box{
    background-color: #fff;
    width: 23%;
    height: 100%;
    float: left;
    position: relative;
    margin-right: 2%;
    border-radius: 3px;
    box-shadow: 0 0 5px 1px rgba(83, 83, 83, 0.1);
  }
  .top_title_box{
    min-width: 22px;
    width: 2%;
    float: left;
    height: 100px; 
    background-color: #1e4dc6;
    text-align: center;
  }
  .top_title{
    color: #fff;
    font-size: 14px;
    height: 100%;
    display: inline-block;
    width: 20px;
  }
  .top_data_name{
    font-size: 14px;
    margin-top: 10px;
  }
  .top_data_content{
    font-size: 24px;
    margin-top: 10px;
  }
  .top_data_infos_color{
    display: inline-block;
    background-color: #ff9e5c;
    width: 10px;
    height: 10px;
    border-radius: 60px;
  }
  .top_data_infos_content{
    display: inline-block;
    font-size: 13px;
    margin-left: 5px;
    margin-top: 5px;
  }
  .midel_echart_box{
    margin-top: 30px;
    height: 300px;
  }
  .midel_echart{
    height: 100%;
    float: left;
    background-color: #fff;
    border-radius: 3px;
    box-shadow: 0 0 5px 1px rgba(83, 83, 83, 0.1);
  }
  .midel_echart_title_box{
    position: relative;
    height: 30px;
    margin-top: 10px;
    width: 100%;
    display: inline-block;
  }
  .midel_echart_title_icon{
    position: absolute;
    left: 15px;
    height: 18px;
    top: 6px;
  }
  .midel_echart_title{
    position: absolute;
    line-height: 34px;
    height: 100%;
    font-size: 15px;
    color: #42484e;
    left: 40px;
  }
  .midel_echart_content{
    width: 100%;
    height: calc(100% - 40px);
  }
  .bottom_echart_box{
    border-radius: 3px;
    box-shadow: 0 0 5px 1px rgba(83, 83, 83, 0.1);
    width: 100%;
    height: 400px;
    background-color: #fff;
    margin-top: 30px;
  }
</style>